﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace OrmSmackdown
{
    public class Performance
    {
        public enum CRUDAction
        {
            Create,
            Read,
            Update,
            Delete,
            InsertAll
        }

        public static void LogPerformance(string ormName, CRUDAction actionType, decimal ms)
        {
            //If it's taking more than 100ms assume this was the first
            //transaction and we are still loading data.
            if (ms > 100.0m)
                return;

            using (var cmd = new SqlCommand("insert into Performance(OrmName, Action, Milliseconds) values (@ormName, @action, @ms)"))
            {
                cmd.Parameters.AddWithValue("@ormName", ormName);
                cmd.Parameters.AddWithValue("@action", actionType.ToString());
                cmd.Parameters.AddWithValue("@ms", ms);

                using (cmd.Connection = new SqlConnection(SqlServerDSN))
                {
                    cmd.Connection.Open();
                    cmd.ExecuteNonQuery();
                }
            }


        }

        private static string SqlServerDSN
        {
            get { return System.Configuration.ConfigurationManager.ConnectionStrings["ssdsn"].ConnectionString; }
        }
    }
}
